package com.dgut.app.controller;

import com.dgut.app.param.MoneyRecordParam;
import com.dgut.app.param.RegisterParam;
import com.dgut.app.service.UserService;
import com.dgut.common.param.UserAuthParam;
import com.dgut.common.utils.R;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author IT00ZYQ
 * @Date 2022/1/5 10:59
 **/
@RequestMapping("/api/app/user")
@RestController
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 微信用户注册
     * @param param 注册信息
     * @return token
     */
    @PostMapping("/register")
    public R register(@RequestBody @Valid RegisterParam param) {
        return R.ok().data(userService.register(param));
    }

    /**
     * 微信用户登录
     * @param code 微信临时票据
     * @return token
     */
    @PostMapping("/login/{code}")
    public R login(@PathVariable("code") String code) {
        return R.ok().data(userService.login(code));
    }

    /**
     * 根据token获取用户信息
     * @param token
     * @return
     */
    @GetMapping("/info")
    public R info(@RequestHeader("token") String token){
        return R.ok().data(userService.userInfo(token));
    }


    /**
     * 用户认证
     */
    @PostMapping("/auth")
    public R auth(@RequestBody @Valid UserAuthParam param, @RequestHeader("token") String token) {
        userService.userAuth(param, token);
        return R.ok();
    }


    /**
     * 回收员资格申请
     */
    @PostMapping("/collector/auth/{reason}")
    @PreAuthorize("hasAnyRole('1')")
    public R auth(@PathVariable("reason") String reason, @RequestHeader("token") String token) {
        userService.collectorAuth(reason, token);
        return R.ok();
    }


    /**
     * 回收员资格申请信息
     */
    @PreAuthorize("hasAnyRole('1,2')")
    @GetMapping("/collector/auth")
    public R authInfo(@RequestHeader("token") String token) {
        return R.ok().data(userService.collectorAuthInfo(token));
    }

    /**
     * 流水记录
     */
    @GetMapping("/money/record")
    @PreAuthorize("hasAnyRole('1,2')")
    public R moneyRecord(MoneyRecordParam param, @RequestHeader("token") String token) {
        return R.ok().data(userService.moneyRecord(param, token));
    }


    /**
     * 余额提现
     */
    @PostMapping("/money/withdraw/{amount}")
    @PreAuthorize("hasAnyRole('1,2')")
    public R withdraw(@PathVariable("amount") Integer amount, @RequestHeader("token") String token) {
        userService.withdraw(amount, token);
        return R.ok();
    }

}
